package cn.iocoder.devlop.module.annual.report.overduepay.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.overduepay.controller.vo.OverduepayCreateReqVO;
import cn.iocoder.devlop.module.annual.report.overduepay.controller.vo.OverduepayOverwriteVO;
import cn.iocoder.devlop.module.annual.report.overduepay.convert.OverduepayConvert;
import cn.iocoder.devlop.module.annual.report.overduepay.dal.OverduepayMapper;
import cn.iocoder.devlop.module.annual.report.overduepay.dal.dataobject.OverduepayDO;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 逾期尚未支付款项信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OverduepayServiceImpl implements OverduepayService {

    @Resource
    private OverduepayMapper overduepayMapper;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private BaseinfoService baseinfoService;

    @Override
    public List<OverduepayDO> getOverduepayList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return overduepayMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteOverduepayAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<OverduepayDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(OverduepayDO::getAncheid,ancheid);
        overduepayMapper.delete(wrapperX);
    }

    @Override
    public void createOverduepayByList(String regno, String ancheyear, List<OverduepayCreateReqVO> overduepayList) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfoService.getMarpripinfo(regno).getEnttype());
        if (CollUtil.isNotEmpty(overduepayList)) {
            List<OverduepayDO> overduepayDOS = OverduepayConvert.INSTANCE.convertList01(overduepayList);
            for (OverduepayDO overduepay : overduepayDOS) {
                overduepay.setAncheid(ancheid);
                overduepay.setAncheoid(IdUtil.simpleUUID());
                overduepay.setEntclassify(entclassify);
            }
            overduepayMapper.insertBatch(overduepayDOS);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteOverduepay(OverduepayOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<OverduepayDO> oldOverduepayList = getOverduepayList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<OverduepayCreateReqVO> newAlterstockinfoList = updateReqVO.getOverduepayList();
                List<OverduepayCreateReqVO> oldreqs = OverduepayConvert.INSTANCE.convertList3(oldOverduepayList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteOverduepayAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createOverduepayByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getOverduepayList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }

}
